package day1;

import java.util.*;

public class ch1 {

    //1
    public int[] twoSum(int[] nums, int target) {

        HashMap<Integer,Integer> map = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            int n = target=nums[i];
            if(map.containsKey(n)){
                return new int[]{map.get(n),i};
            }
            map.put(nums[i],i);
        }
        return new int[]{0,0};
    }

    //49
    public List<List<String>> groupAnagrams(String[] strs) {

        HashMap<String,List<String>> map = new HashMap<>();
        List<List<String>> list = new ArrayList<>();

        for(String str:strs){
            char[] ch=str.toCharArray();
            Arrays.sort(ch);
            String index =  new String(ch);
            if(!map.containsKey(index)){
                map.put(index,new ArrayList<String>());
            }
            map.get(index).add(str);
        }

        for(String str:map.keySet()){
            list.add(map.get(str));

        }

        return list;


    }

    //128
    public int longestConsecutive(int[] nums) {

        HashMap<Integer,Integer> map = new HashMap<>();
        for(int i:nums){
            map.put(i,-1);
        }
        int max = 0;
        for(int i:map.keySet()) {
            if(map.get(i)==-1){
                int num = i;
                int len = 1;

                while (map.containsKey(num+1)) {
                    int x = map.get(num+1);
                    map.put(num+1,1);
                    if (x == -1) {
                        len += 1;
                        num += 1;
                    } else {
                        len += x;
                        break;
                    }
                }
                map.put(i,len);

                max = Math.max(max, len);
            }
            }



        return  max;

    }




    public static void main(String[] args) {

        String str = "bac";
        char[] ch=str.toCharArray();
        Arrays.sort(ch);
        String index = new String(ch.toString());
        System.out.println(index);

        String str1 = "acb";
        char[] ch1=str.toCharArray();
        Arrays.sort(ch1);

        for(char c:ch1){
            System.out.println(c);
        }

        String index1 = new String(ch1);
        System.out.println(index1);



    }
}
